//
//  WDRightView.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2017/3/14.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "WDRightView.h"

@interface WDRightView ()
/** 数据源 */
@property (strong, nonatomic) NSMutableArray *data;
/** 临时按钮 */
@property (strong, nonatomic) UIButton *tempBtn;

@end

CGFloat btnW = 20;

@implementation WDRightView

- (NSMutableArray *)data
{
    if (!_data) {
        
        _data = [NSMutableArray array];
        
        [_data addObject:@"全\n部"];
        
        for (char i = 'A'; i <= 'Z'; i++) {
            
            NSString *str = [NSString stringWithFormat:@"%c", i];
            
            [_data addObject:str];
        }
    }
    
    return _data;
}

+ (instancetype)rightView
{
    WDRightView *rightView = [[WDRightView alloc] init];
    
    return rightView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = ColorWithAlpha(0x000000, 0.1);
        
        [self loadBtns];
    }
    
    return self;
}

- (void)loadBtns
{
    
    UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    tempBtn.frame = CGRectMake(0, 10, btnW, 32);
    
    tempBtn.tag = 0;
    
    tempBtn.selected = YES;
    
    [tempBtn setTitle:self.data[0] forState:UIControlStateNormal];
    
    tempBtn.titleLabel.numberOfLines = 0;
    
    [tempBtn setTitleColor:ColorForRGB(0x6b6b6b) forState:UIControlStateNormal];
    
    [tempBtn setTitleColor:ColorForRGB(0xfd5722) forState:UIControlStateSelected];
    
    tempBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    
    tempBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [tempBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tempBtn = tempBtn;
    
    [self addSubview:tempBtn];
    
    for (int i = 1; i < self.data.count; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        btn.frame = CGRectMake(0, i * WDScale(17) +WDScale(30), btnW, 16);
        
        btn.tag = i;
        
        [btn setTitle:self.data[i] forState:UIControlStateNormal];
        
        btn.titleLabel.numberOfLines = 0;
        
        [btn setTitleColor:ColorForRGB(0x6b6b6b) forState:UIControlStateNormal];
        
        [btn setTitleColor:ColorForRGB(0xfd5722) forState:UIControlStateSelected];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:btn];
    }
}

- (void)clickBtn:(UIButton *)sender
{
    if (self.tempBtn != sender) {
        
        self.tempBtn.selected = NO;
        
        self.tempBtn = sender;
        sender.selected  = YES;
    }
    
    if (sender.selected) {
        WDLog(@"选中第%zd行的%@", sender.tag, sender.titleLabel.text);
        [_delegate rightView:self clickStr:[sender.titleLabel.text lowercaseString]];
    }
}


@end
